package edu.ycxy.poetry.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.ycxy.poetry.entity.Ju;
import edu.ycxy.poetry.vo.AddSentenceVo;
import edu.ycxy.poetry.vo.UpdateSentenceVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author @xin
 * @createDate 2022/3/8
 * @Description
 */
@Repository
public interface JuMapper extends BaseMapper<Ju> {
    @Select("select * from vab_ju")
    List<Ju> selectListJu();

    //根据名句表中的gid查询该名句的作者
    @Select("select zuozhe from vab_shiwen where id=#{id}")
    String findNameByGid(Integer id);

    //根据名句的gid查询名句内容
    @Select("select title from vab_ju where gid=#{gid} and title=#{title}")
    String findTitleByGid(Integer gid,String title);

    //获取名句的前30条名句
    @Select("select * from vab_ju order by id limit 0,30")
    List<Ju> selectJus();

    /**
     * @后台操作
     * @return
     */
    // 向名句插入数据
    @Insert("insert into vab_ju(title,time,biaoti,gid) values (#{title},(select UNIX_TIMESTAMP(NOW())),#{biaoti},(select id from vab_writer where name=#{author}))")
    Integer insertOne(String title,String biaoti,String author);

    // 模糊查询
    @Select("select * from vab_ju where title like CONCAT('%',#{title},'%')")
    List<Ju> findListByTileToLike(String title);

    //根据id删除名句信息
    @Delete("delete from vab_ju where id=#{id}")
    Integer deleteJuById(Integer id);

    //根据id修改名句信息
    @Update("update vab_ju set title=#{sentence},time=(select UNIX_TIMESTAMP(NOW())),gid=(select id from vab_writer where name=#{author}),biaoti=#{poemName} where id=#{id}")
    Integer updateJuById(UpdateSentenceVo updateSentenceVo);

    // 根据名句id查询名句信息
    @Select("select * from vab_ju where id=#{id}")
    Ju findOneById(Integer id);
}


